//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.Caching;
using System.IO;
using System.Xml;
 
// CS
using CommunityServer.Components;
using CommunityServer.Blogs.Components;
 
namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for CommentHandler.
	/// </summary>
	///
	// jayson.knight -- fix for posting comments from feed readers
	public class RssCommentHandler : IHttpHandler
	{
		public RssCommentHandler() { }
 
		#region IHttpHandler Members
 
		public void ProcessRequest(HttpContext context)
		{
			HttpRequest request = context.Request;
			if(request.RequestType == "POST" && request.ContentType == "text/xml")
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(request.InputStream);
 
				User user = Users.GetUser();
				int postID = int.Parse(request.QueryString["postid"]); // getPostIDFromUrl(request.RawUrl);
 
				WeblogPost commentedEntry = WeblogPosts.GetPost(postID, false, true, false);
				Weblog blog = commentedEntry.Section as Weblog;
				 
				Permissions.AccessCheck(blog, Permission.View, user);
 
				// if comments aren't enabled, throw an http forbidden exception
				if (!blog.EnableCommentsDefault)
				{
					throw new HttpException(403, "Comments are not enabled.");
				}

				// if rss comments aren't enabled, throw an http forbidden exception
				if (!blog.EnableRssCommentsDefault)
				{
					throw new HttpException(403, "Rss Comments are not enabled.");
				}
 
				string name = doc.SelectSingleNode("//item/author").InnerText;
				if(name.IndexOf("<") != -1)
				{
					name = name.Substring(0,name.IndexOf("<"));
				}
 
				WeblogPost post = new WeblogPost();
				post.SubmittedUserName = name.Trim();
				post.BlogPostType = BlogPostType.Comment;
				post.SectionID = blog.SectionID;
				post.ParentID = postID;
				post.Body = doc.SelectSingleNode("//item/description").InnerText;
				post.Subject = doc.SelectSingleNode("//item/title").InnerText;
				post.TitleUrl = checkForUrl(doc.SelectSingleNode("//item/link").InnerText);
				post.IsApproved = !blog.IsPostModerated(commentedEntry, user); ;
				post.PostDate = DateTime.Now;
				post.UserTime = DateTime.Now;
				WeblogPosts.Add(post, user);
			}
			else
			{
                       
			}
		}
 
		private string checkForUrl(string text)
		{
			if (text == null || text.Trim().Length == 0)
			{
				return string.Empty;
			}

			if (text.Trim().ToLower().StartsWith("http://") || text.Trim().ToLower().StartsWith("https://"))
			{
				return text;
			}
			return "http://" + text;
		}
 
		//private int getPostIDFromUrl(string uri)
		//{
		//    try
		//    {
		//        return Int32.Parse(getReqeustedFileName(uri));
		//    }
		//    catch (FormatException)
		//    {
		//        throw new ArgumentException("Invalid Post ID.");
		//    }
		//}
 
		//private string getReqeustedFileName(string uri)
		//{
		//    return Path.GetFileNameWithoutExtension(uri);
		//}
 
		public bool IsReusable
		{
			get
			{
				return true;
			}
		}
 
		#endregion
	}
}